import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:kunlun_gas/AppViews/MainView/widget/main_item_widget.dart';
import '../../../app_files.dart';
import 'action.dart';
import 'state.dart';

Widget buildView(
    EngineerListViewState state, Dispatch dispatch, ViewService viewService) {
  List<String> sortTitles = [
    '新任务',
    '已接任务',
    GlobalStore.store.getState().userInfo.isEngineer ? '安装中' : '维修中',
    GlobalStore.store.getState().userInfo.isEngineer ? '已安装' : '已维修'
  ];
  Widget _sortWidget() {
    List<Widget> items = sortTitles.map<Widget>((e) {
      int index = sortTitles.indexOf(e);
      return Expanded(
        child: Container(
          color:
          state.currentSortIndex == index ? ThemeConfig.mainColor : Colors.white,
          child: InkWell(
            child: Align(
              child: Text(
                e,
                style: TextStyle(
                  color: state.currentSortIndex == index
                      ? Colors.white
                      : ThemeConfig.blackColor,
                  fontSize: 14,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
            onTap: () =>
                dispatch(EngineerListViewActionCreator.onTapSort(index)),
          ),
        ),
      );
    }).toList();

    return Container(
      margin: EdgeInsets.only(top: 0),
      height: 50,
      width: double.infinity,
      color: Colors.white,
      child: Row(
        children: items,
      ),
    );
  }

  Widget safeBuilder(BuildContext context, int index) {
    if (state.items.length == 0)
      return Container(
        child: Center(
          child: Stack(
            children: <Widget>[
              MyEmptyItemWidget(),
            ],
          ),
        ),
      );

    FixListItemModel item = state.items[index];
    return GlobalStore.store.getState().userInfo.isEngineer
        ? MainItemWidget.engineerBuilder(context, item)
        : MainItemWidget.fixBuilder(context, item);
  }

  return Scaffold(
    appBar: myAppBar(viewService.context, title: state.type == 5 ? '业务工单' :'维修工单'),
    body: Column(
      children: [
        _sortWidget(),
        Expanded(
          child: Container(
            child: state.items == null
                ? Center(
              child: MyLoadingWidget(),
            )
                : CustomRefreshList(
              onRefresh: (_) =>
                  dispatch(EngineerListViewActionCreator.onLoad(1)),
              onLoading: (_,page)=>    dispatch(EngineerListViewActionCreator.onLoad(page)),
              currentPage: state.currentPage,
              refreshController: state.refreshController,
              child: ListView.builder(
                itemBuilder: safeBuilder,
                itemCount:
                state.items.length == 0 ? 1 : state.items.length,
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
              ),
            ),
          ),
        ),
      ],
    ),
  );
}
